package cdu.sl.config;

import cdu.sl.interceptor.JwtInterceptor;
import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Web配置类
 */
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(AuthProperties.class)
public class WebMvcConfig implements WebMvcConfigurer {

    private final JwtInterceptor jwtInterceptor;

    private final AuthProperties authProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器
        InterceptorRegistration registration = registry.addInterceptor(jwtInterceptor);

        // 配置需要拦截的路径
        List<String> includePaths = authProperties.getIncludePaths();
        if(CollUtil.isNotEmpty(includePaths)){
            registration.addPathPatterns(includePaths);
        }

        // 配置需要放行的路径
        List<String> excludePaths=authProperties.getExcludePaths();
        if(CollUtil.isNotEmpty(excludePaths)){
            registration.excludePathPatterns(excludePaths);
        }
        registration.excludePathPatterns(
                "/error",
                "/favicon.ico",
                "/v2/**",
                "/v3/**",
                "/swagger-resources/**",
                "/webjars/**",
                "/doc.html"
        );


    }


}